#include "mainwidget.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QSpinBox>

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent)
{
    setLayout(new QVBoxLayout(this));
    booksCount = new QProgressBar(this);
    activePeopleCount = new QProgressBar(this);
    passivePeopleCount = new QProgressBar(this);

    readersCount = 5;
    writersCount = 5;
    readInterval = 2;
    writeInterval = 1;
    maxBooksCount = 20;

    activePeopleCount->setValue(readersCount + writersCount);
    passivePeopleCount->setValue(0);
    booksCount->setValue(0);
    activePeopleCount->setMaximum(readersCount + writersCount);
    passivePeopleCount->setMaximum(readersCount + writersCount);
    booksCount->setMaximum(maxBooksCount);

    shelf::books.reserve(maxBooksCount);

    QLabel* booksLabel = new QLabel("Number of books");
    QLabel* activePeopleLabel = new QLabel("Number of active people");
    QLabel* passivePeopleLabel = new QLabel("Number of passive people");

    QLabel* readLabel = new QLabel("Read interval");
    QLabel* writeLabel = new QLabel("Write interval");

    readIntervalBox = new QSpinBox();
    readIntervalBox->setMinimum(0);
    readIntervalBox->setMaximum(10);
    readIntervalBox->setValue(2);
    connect(readIntervalBox, SIGNAL(valueChanged(int)), this, SLOT(changeReadInterval(int)));

    writeIntervalBox = new QSpinBox();
    writeIntervalBox->setMinimum(0);
    writeIntervalBox->setMaximum(10);
    writeIntervalBox->setValue(1);
    connect(writeIntervalBox, SIGNAL(valueChanged(int)), this, SLOT(changeWriteInterval(int)));

    layout()->addWidget(booksLabel);
    layout()->addWidget(booksCount);
    layout()->addWidget(activePeopleLabel);
    layout()->addWidget(activePeopleCount);
    layout()->addWidget(passivePeopleLabel);
    layout()->addWidget(passivePeopleCount);

    layout()->addWidget(readLabel);
    layout()->addWidget(readIntervalBox);
    layout()->addWidget(writeLabel);
    layout()->addWidget(writeIntervalBox);
}

void MainWidget::startWork()
{
    pool = QThreadPool::globalInstance();
    pool->setMaxThreadCount(readersCount + writersCount + 1);

    for(size_t i = 0; i < writersCount; ++i)
    {
        Writer* writer = new Writer(writeInterval);
        connect(writer, SIGNAL(changedBooksCount(int)), this, SLOT(changeBooksCount(int)));
        connect(writer, SIGNAL(changePeopleCount(int)), this, SLOT(changePeopleCount(int)));
        pool->start(writer);
    }
    for(size_t i = 0; i < readersCount; ++i)
    {
        pool->start(new Reader(readInterval));
    }
}

void MainWidget::changePeopleCount(int diff)
{
    activePeopleCount->setValue(activePeopleCount->value() + diff);
    passivePeopleCount->setValue(passivePeopleCount->value() - diff);
}

void MainWidget::changeBooksCount(int diff)
{
    booksCount->setValue(booksCount->value() + diff);
}

void MainWidget::changeReadInterval(int value)
{
    readInterval = value;
}

void MainWidget::changeWriteInterval(int value)
{
    writeInterval = value;
}
